#include <Arduino.h>
#include "config.h"
#include "wifi_init.h"
#include "mqtt.h"
#include "gpio_cfg.h"
#include "weight_hx711.h"
#include <esp_wifi.h>
#include "otaconfig.h"

StaticJsonDocument<200> json_finish;
uint8_t work_state_temp = 0 ;

// void pow_manage(void *pvParameters){
//   while(1){
//     if(pow_state==1){
//       if(millis()-control_time>600000){
//         digitalWrite(KEY6,LOW);
//         delay(200);
//         digitalWrite(KEY6,HIGH);
//         pow_state=0;
//       }
//     }
//   }
// }

void storage_worktimes(){
  pref.begin("config");
  pref.putUInt("work_times",work_times);
  pref.end();
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  gpio_init();
  pref.begin("config");
  if(pref.isKey("SSID")){
    SSID=pref.getString("SSID");
  }
  if(pref.isKey("PASSWD")){
    PASSWD=pref.getString("PASSWD");
  }
  pref.end();
  // hx711_init();
  controll_time_init();
  chipid=ESP.getEfuseMac();
  WiFi.macAddress(mac_uint8);
  Serial.printf("Firmware version:%s\n",VERSION);
  sprintf(mac, "%02x%02x%02x%02x%02x%02x", mac_uint8[0], mac_uint8[1], mac_uint8[2], mac_uint8[3], mac_uint8[4], mac_uint8[5]);
  // strcpy(mac,WiFi.macAddress().c_str());
  bleconfigSemaphore = xSemaphoreCreateBinary();//蓝牙配网二值信号量
  WIFI_init();
  mqtt_init();
  attempt_ota();
  xTaskCreatePinnedToCore(mqtt_loop,    //
                  "mqtt_loop",  //任务名称
                  0x1000,       //堆栈大小
                  NULL,       //输入参数
                  1,          //任务优先级
                  NULL,       //
                  0           //核心  0\1
                  );
  xTaskCreatePinnedToCore(key_monitor,    //
                  "key_monitor",  //任务名称
                  0x1000,       //堆栈大小
                  NULL,       //输入参数
                  2,          //任务优先级
                  NULL,       //
                  0           //核心  0\1
                  );
}

void loop() {
    if(work_state!=0){
      work_state_temp = work_state;
      control_time=millis();
      printf("work state  %d\n",work_state);
      if(work_state==1){
        digitalWrite(VALVE1,HIGH);
        digitalWrite(KEY1,HIGH);
        delay(200);
        digitalWrite(KEY1,LOW);
        uint remintime=kabuqinuo_time;
        while((remintime>100)&&(work_state!=0)){
          delay(100);
          remintime-=100;
        if (work_state == 0)
            {
              break;
            }
        }
        digitalWrite(VALVE1,LOW);
        work_times+=1;
        storage_worktimes();
      }
      else if(work_state==2){
        digitalWrite(KEY3,HIGH);
        delay(200);
        digitalWrite(KEY3,LOW);
        uint remintime=yishi_time;
        while((remintime>100)&&(work_state!=0)){
          delay(100);
          remintime-=100;
          if (work_state == 0)
              {
                break;
              }
        }
        // delay(50000);
        work_times+=1;
        storage_worktimes();
      }
      else if(work_state==3){
        digitalWrite(KEY2,HIGH);
        delay(200);
        digitalWrite(KEY2,LOW);
        uint remintime=clean_time;
        while((remintime>100)&&(work_state!=0)){
          delay(100);
          remintime-=100;
        }
        digitalWrite(KEY4,HIGH);
        delay(200);
        digitalWrite(KEY4,LOW);
        delay(2000);
        // work_times+=1;
        // storage_worktimes();
      }
      else if(work_state==4){
        digitalWrite(VALVE1,HIGH);
        digitalWrite(KEY5,HIGH);
        delay(200);
        digitalWrite(KEY5,LOW);
        uint remintime=natie_time;
        while((remintime>100)&&(work_state!=0)){
          delay(100);
          remintime-=100;
          if (work_state == 0)
              {
                break;
              }
        }
        digitalWrite(VALVE1,LOW);
        work_times+=1;
        storage_worktimes();
      }
      else if(work_state==5){

        if(pow_state==1){
          digitalWrite(KEY6,HIGH);
          delay(3000);
          digitalWrite(KEY6,LOW);
          delay(2000);
          esp_restart();
        }else{
        digitalWrite(KEY6,HIGH);
        delay(200);
        digitalWrite(KEY6,LOW);
        }
        pow_state=1;
      }
      else if(work_state==6){
        digitalWrite(KEY4,HIGH);
        delay(200);
        digitalWrite(KEY4,LOW);
      }
      else if(work_state==7){
        hall_count=0;
      set_pump_speed(PWM_CHANNEL1,100);
      set_pump_speed(PWM_CHANNEL2,100);
      uint remintime=max_time;
        while((remintime>100)&&(hall_count<channel1_pluse)){
          delay(10);
          remintime-=10;
        }
        Serial.printf("water_pluse:%d\n",hall_count);
      set_pump_speed(PWM_CHANNEL1,0);
      set_pump_speed(PWM_CHANNEL2,0);
      }
      else if(work_state==8){
        hall_count=0;
      set_pump_speed(PWM_CHANNEL2,100);
      uint remintime=max_time;
        while((remintime>100)&&(hall_count<channel1_pluse)){
          delay(10);
          remintime-=10;
        }
        Serial.printf("water_pluse:%d\n",hall_count);
      set_pump_speed(PWM_CHANNEL2,0);
      }
      else if(work_state==9){
          hall_count=0;
      set_pump_speed(PWM_CHANNEL3,50);
        while(hall_count<yeru_pluse){
          delay(10);
        }
      set_pump_speed(PWM_CHANNEL3,0);
      }
      else if(work_state==10){
          hall_count=0;
      set_pump_speed(PWM_CHANNEL4,50);
        while(hall_count<yeru_pluse){
          delay(10);
        }
      set_pump_speed(PWM_CHANNEL4,0);
      }
      else if(work_state==11){
        Serial.println(mqtt_jzz);
        caculate_scale(2,mqtt_jzz);
      }
      else if(work_state==12){
        // digitalWrite(PUMP,HIGH);
        set_pump_speed(PWM_CHANNEL,50);
      }
      else if(work_state==13){
        // digitalWrite(PUMP,LOW);
        set_pump_speed(PWM_CHANNEL,0);
      }
      else if(work_state==14){
        digitalWrite(VALVE1,HIGH);
      }
      else if(work_state==15){
        digitalWrite(VALVE2,HIGH);
      }
      else if(work_state==16){
        digitalWrite(VALVE1,LOW);
      }
      else if(work_state==17){
        digitalWrite(VALVE2,LOW);
      }
      else if(work_state==18){
        Serial.println(natie_time);
        pref.begin("config");
        pref.putUInt("natie_time",natie_time);
        pref.end();
      }
      else if(work_state==19){
        Serial.println(kabuqinuo_time);
        pref.begin("config");
        pref.putUInt("kabuqinuo_time",kabuqinuo_time);
        pref.end();
      }
      else if(work_state==20){
        Serial.println(yishi_time);
        pref.begin("config");
        pref.putUInt("yishi_time",yishi_time);
        pref.end();
      }
      else if(work_state==21){
        Serial.println(clean_time);
        pref.begin("config");
        pref.putUInt("clean_time",clean_time);
        pref.end();
      }
      else if(work_state==22){
        // Serial.println(yeru_pluse);
        pref.begin("config");
        pref.putUInt("yeru_pluse",yeru_pluse);
        pref.end();
      }
      else if(work_state==23){
        pref.begin("config");
        pref.putUInt("work_times",work_times);
        pref.end();
      }
      else if(work_state==24){//冰美式
        hall_count=0;
        set_pump_speed(PWM_CHANNEL1,100);
        set_pump_speed(PWM_CHANNEL2,100);
      uint remintime=max_time;
        while((remintime>100)&&(hall_count<channel1_pluse)){
          delay(10);
          remintime-=10;
        }
        Serial.printf("water_pluse:%d\n",hall_count);
        set_pump_speed(PWM_CHANNEL1,0);
        set_pump_speed(PWM_CHANNEL2,0);
        delay(200);
        digitalWrite(KEY3,HIGH);
        delay(200);
        digitalWrite(KEY3,LOW);
        uint remin_time=yishi_time;
        while((remintime>100)&&(work_state!=0)){
          delay(100);
          remin_time-=100;
          if (work_state == 0)
              {
                break;
              }
        }
        // delay(50000);
        work_times+=1;
        storage_worktimes();
      }
      else if(work_state==26){
        if(pow_state==0){
          digitalWrite(KEY6,HIGH);
          delay(200);
          digitalWrite(KEY6,LOW); 
          uint16_t countpow = 0;
          while (pow_state==0)//这里改一下，不是0写一个状态
          {
            delay(100);
            countpow+=1;
            if (countpow >= 600)
            {
              break;
            }
            
            /* code */
          }
          Serial.printf("power on");
          pow_state=1;
        }
        else{
          digitalWrite(KEY6,HIGH);
          delay(3000);
          digitalWrite(KEY6,LOW); 
          uint16_t countpow = 0;
          while (pow_state==1)//这里改一下，不是0写一个状态
          {
            delay(100);
            countpow+=1;
            if (countpow >= 800)
            {
              break;
            }
            
            /* code */
          }
          Serial.printf("power off,power state:%d,work state,%d",pow_state,work_state);
          pow_state=0;
          delay(1000);
          digitalWrite(KEY6,HIGH);
          delay(200);
          digitalWrite(KEY6,LOW); 
          countpow = 0;
          while (pow_state==0)//这里改一下，不是0写一个状态
          {
            delay(100);
            countpow+=1;
            if (countpow >= 800)
            {
              break;
            }
            
            /* code */
          }
          Serial.printf("power on,power state:%d,work state,%d",pow_state,work_state);
          pow_state=1;
        }
      }
      // delay(1000);
      
      json_finish.clear();
      json_finish["state"]="finish";
      json_finish["io"]=work_state_temp;
      char state_doc[measureJson(json_finish) + 1];
      serializeJson(json_finish, state_doc, measureJson(json_finish) + 1);
      mqtt_client.publish(mqtt_pub_topic,state_doc);
      work_state=0;
      work_state_temp = 0;
      Serial.printf("work finish. work times %d\n",work_times);
    }
    // if((work_times%10==0)&&door_reset==false){
    if(door_reset==false){
      Serial.println("open door");
      digitalWrite(DOOR_SWICH,HIGH);
      delay(3000);
      digitalWrite(DOOR_SWICH,LOW);
      door_reset=true;
    }
    // if((work_times%10!=0)&&door_reset==true){
    //   door_reset=false;
    // }
    delay(100);
    // Serial.printf("%d\n",hall_count);
}
//生成一个产生随机数的函数
